package com.xxx.mapper;

import com.xxx.eneity.User;
import com.xxx.vo.UserVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select count(*) from t_user where username = #{username}")
    public int getUserCount(@Param("username") String username);
    @Select("select id, username, name, email, role, sex, number, major, class_name as className, info from t_user where username = #{username} and password = #{password}")
    public User getUser(@Param("username") String username, @Param("password") String password);

    @Insert("insert into t_user (username, password, name, email, role, sex, number, major, class_name, info) values " +
            "(#{username}, #{password}, #{name}, #{email}, #{role}, #{sex}, #{number}, #{major}, #{className}, #{info})")
    public int register(User user);

    @Select("select * from t_user where id = #{userId}")
    public User getUserById(Integer userId);

    @Select("select count(*) from t_user where id = #{userId} and password = #{oldpwd}")
    public int getCount(Integer userId, String oldpwd);

    @Update("update t_user set password = #{newpwd} where id = #{userId}")
    public int editPassword(Integer userId, String newpwd);

    @Update("<script>" +
            "update t_user " +
            "<trim prefix='set' suffixOverrides=','>" +
            "<if test=\"name != null and name != ''\">" +
            "name = #{name}, " +
            "</if>" +
            "<if test=\"email != null and email != ''\">" +
            "email = #{email}, " +
            "</if>" +
            "<if test=\"sex != null and sex != ''\">" +
            "sex = #{sex}, " +
            "</if>" +
            "<if test=\"number != null and number != ''\">" +
            "number = #{number}, " +
            "</if>" +
            "<if test=\"major != null and major != ''\">" +
            "major = #{major}, " +
            "</if>" +
            "<if test=\"className != null and className != ''\">" +
            "class_name = #{className}, " +
            "</if>" +
            "<if test=\"info != null and info != ''\">" +
            "info = #{info} " +
            "</if>" +
            "</trim>" +
            "where id = #{id}" +
            "</script>")
    public int editUser(User user);

    @Select("select id, name from t_user where role = 1")
    public List<UserVo> getUserList();


    @Select("select count(*) from t_all_user where number = #{code}")
    public int getCountByCode(Integer code);

    @Update("update t_all_user set isRegistered = 1, user_id = #{id} where number = #{code}")
    public void updateCode(User user);

    @Select("select role from t_all_user where number = #{code}")
    public Integer getRole(Integer code);

    @Select("select id from t_user where username = #{username}")
    public Integer getUserId(User user);

    @Select("select isRegistered from t_all_user where number = #{code}")
    public int isCodeRegistered(Integer code);
}
